package com.guigu.www.erp.tangyingmeng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.www.erp.common.pojo.DFile;
import com.guigu.www.erp.common.pojo.PageVo;
import com.guigu.www.erp.tangyingmeng.dao.DFileMapper;
import com.guigu.www.erp.tangyingmeng.service.TYMDFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.awt.image.ImageAccessException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TYMDFileServiceImpl implements TYMDFileService {

    @Autowired
    DFileMapper mapper;


    @Override
    public int DfileInsert(DFile dFile,String username) {
        String bian ="100010101";
        String[] firsts = dFile.getFirstKindId().split("/");
        dFile.setFirstKindId(firsts[0]);
        dFile.setFirstKindName(firsts[1]);
        String[] seconds = dFile.getSecondKindId().split("/");
        dFile.setSecondKindId(seconds[0]);
        dFile.setSecondKindName(seconds[1]);
        String[] thirds = dFile.getThirdKindId().split("/");
        dFile.setThirdKindId(thirds[0]);
        dFile.setThirdKindName(thirds[1]);
        String id = mapper.getproductId();
        if(id!=null){
            Integer i = Integer.parseInt(id.substring(9));
            i++;
            String str=i+"";
            switch (str.length()){
                case 1:
                    str="00000"+str;
                    break;
                case 2:
                    str="0000"+str;
                    break;
                case 3:
                    str="000"+str;
                    break;
                case 4:
                    str="00"+str;
                    break;
                case 5:
                    str="0"+str;
                    break;
            }
            bian+=str;
        }else {
            bian+="000001";
        }
        dFile.setProductId(bian);
        Date date = new Date();
        dFile.setRegisterTime(date.toLocaleString());
        dFile.setCheckTag("0");
        dFile.setChangeTag("0");
        dFile.setPriceChangeTag("0");
        dFile.setDeleteTag("0");
        dFile.setDesignModuleTag("0");
        dFile.setDesignProcedureTag("0");
        dFile.setDesignCellTag("0");
        dFile.setRegister(username);
        dFile.setRealCostPrice(dFile.getCostPrice());
        int insert = mapper.insert(dFile);
        return insert;
    }

    @Override
    public int existence(String productName) {
        return mapper.existence(productName);
    }

    @Override
    public PageVo<DFile> selfuhe(Map<String, String> map,int deptid) {
        Page<DFile> page = PageHelper.startPage(Integer.valueOf(map.get("pageNum")==null?"1":map.get("pageNum")),
                                                Integer.valueOf(map.get("pageSize")==null?"2":map.get("pageSize")));
        List<DFile> list = mapper.selfuhe(deptid);
        PageInfo<DFile> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DFile> attenceList = pageInfo.getList();
        PageVo<DFile> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }

    @Override
    public DFile fuheselbyone(int fileId) {
        return mapper.selectByPrimaryKey(fileId);
    }

    @Override
    public int FuHePiZhun(String productId, String username) {
        DFile dFile = new DFile();
        Date date = new Date();
        dFile.setProductId(productId);
        dFile.setChecked(username);
        dFile.setCheckTime(date.toLocaleString());
        dFile.setCheckTag("1");
        return mapper.FuHePiZhun(dFile);
    }

    @Override
    public int FuHeBuZhun(String productId, String username) {
        DFile dFile = new DFile();
        Date date = new Date();
        dFile.setProductId(productId);
        dFile.setChecked(username);
        dFile.setCheckTime(date.toLocaleString());
        dFile.setCheckTag("2");
        return mapper.FuHePiZhun(dFile);
    }

    @Override
    public PageVo<DFile> selall(Map<String, String> map) {
        Page<DFile> page = PageHelper.startPage(Integer.valueOf(map.get("pageNum")==null?"1":map.get("pageNum")),
                Integer.valueOf(map.get("pageSize")==null?"2":map.get("pageSize")));
        DFile dFile = new DFile();
        dFile.setProductName(map.get("productName"));
        dFile.setType(map.get("type"));
        dFile.setRegisterTime(map.get("kairegisterTime"));
        dFile.setCheckTime(map.get("moregisterTime"));
        List<DFile> list = mapper.selall(dFile);
        PageInfo<DFile> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DFile> attenceList = pageInfo.getList();
        PageVo<DFile> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }

    @Override
    public PageVo<DFile> selsc(Map<String, String> map) {
        Page<DFile> page = PageHelper.startPage(Integer.valueOf(map.get("pageNum")==null?"1":map.get("pageNum")),
                Integer.valueOf(map.get("pageSize")==null?"2":map.get("pageSize")));
        List<DFile> list = mapper.selsc("1");
        PageInfo<DFile> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DFile> attenceList = pageInfo.getList();
        PageVo<DFile> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }

    @Override
    public int updasc(int fileId) {
        return mapper.updasc(fileId);
    }

    @Override
    public PageVo<DFile> selhf(Map<String, String> map) {
        Page<DFile> page = PageHelper.startPage(Integer.valueOf(map.get("pageNum")==null?"1":map.get("pageNum")),
                Integer.valueOf(map.get("pageSize")==null?"2":map.get("pageSize")));
        List<DFile> list = mapper.selhf("1");
        PageInfo<DFile> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DFile> attenceList = pageInfo.getList();
        PageVo<DFile> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }

    @Override
    public int updahf(int fileId) {
        return mapper.updahf(fileId);
    }

    @Override
    public int delda(int fileId) {
        return mapper.deleteByPrimaryKey(fileId);
    }

    @Override
    public PageVo<DFile> selbg(Map<String, String> map) {
        Page<DFile> page = PageHelper.startPage(Integer.valueOf(map.get("pageNum")==null?"1":map.get("pageNum")),
                Integer.valueOf(map.get("pageSize")==null?"2":map.get("pageSize")));
        List<DFile> list = mapper.selbg();
        PageInfo<DFile> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DFile> attenceList = pageInfo.getList();
        PageVo<DFile> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }

    @Override
    public int updabg(DFile dFile, String username) {
        Integer selbgcs = mapper.selbgcs(dFile.getProductId());
        if(selbgcs==null){
            dFile.setFileChangeAmount(1);
        }else {
            dFile.setFileChangeAmount(selbgcs+1);
        }
        Date date = new Date();
        dFile.setChangeTime(date.toLocaleString());
        dFile.setChanger(username);
        dFile.setRealCostPrice(dFile.getCostPrice());
        int updabg = mapper.updabg(dFile);
        return updabg;
    }

    @Override
    public PageVo<DFile> selbywuliao(Map<String, String> map) {
        Page<DFile> page = PageHelper.startPage(Integer.valueOf(map.get("pageNum")==null?"1":map.get("pageNum")),
                Integer.valueOf(map.get("pageSize")==null?"2":map.get("pageSize")));
        List<DFile> list = mapper.selbywuliao();
        PageInfo<DFile> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DFile> attenceList = pageInfo.getList();
        PageVo<DFile> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }

    @Override
    public List<DFile> selwuliao() {
        List<DFile> list = mapper.selwuliao();
        return list;
    }

}
